Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
`unenv` is a framework-agnostic system that allows converting JavaScript code to be platform agnostic and work in any environment including Browsers, Workers, Node.js, or JavaScript runtime.
The 'unenv' npm package is designed to provide a universal environment for JavaScript and TypeScript projects. It allows developers to create isomorphic code that can run seamlessly in both Node.js and browser environments by providing polyfills and shims for various APIs.
Polyfills for Node.js APIs
This feature provides polyfills for Node.js APIs, allowing you to use Node.js-specific features in a browser environment. The code sample demonstrates how to use the Buffer API from Node.js in a universal environment.
const { Buffer } = require('unenv/runtime/polyfills/buffer');
const buffer = Buffer.from('Hello, world!');
console.log(buffer.toString('utf-8'));
Environment Detection
This feature allows you to detect the current environment (Node.js or browser) and execute code conditionally based on that. The code sample shows how to check if the code is running in Node.js or a browser.
const { isNode, isBrowser } = require('unenv');
if (isNode) {
console.log('Running in Node.js');
} else if (isBrowser) {
console.log('Running in the browser');
}
Universal Fetch API
This feature provides a universal Fetch API that works in both Node.js and browser environments. The code sample demonstrates how to make a fetch request and handle the response.
const fetch = require('unenv/runtime/polyfills/fetch');
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));
The 'isomorphic-fetch' package provides a Fetch API implementation that works in both Node.js and browser environments. It is similar to the Fetch API polyfill provided by 'unenv', but it focuses solely on fetch functionality.
The 'universal-env' package helps in detecting the current environment (Node.js or browser) and provides utility functions for environment-specific code. It is similar to the environment detection feature of 'unenv'.
The 'node-polyfill-webpack-plugin' package provides polyfills for Node.js core modules in a Webpack environment. It is similar to the polyfills provided by 'unenv', but it is specifically designed for use with Webpack.
unenv
is a framework-agnostic system that allows converting JavaScript code to be platform agnostic and work in any environment including Browsers, Workers, Node.js, or JavaScript runtime.
[!NOTE] You are on the legacy (v1) branch. Checkout main branch for latest development.
# Using npm
npm i -D unenv
# Using yarn
yarn add --dev unenv
# Using pnpm
pnpm add -D unenv
Using env
utility and built-in presets, unenv
will provide an abstract configuration that can be used in building pipelines (rollup.js, webpack, etc.).
import { env } from "unenv";
const { alias, inject, polyfill, external } = env({}, {}, {});
Note: You can provide as many presets as you want. unenv will merge them internally and the right-most preset has a higher priority.
node
Suitable to convert universal libraries working in Node.js.
fetch
APIimport { env, nodeless } from "unenv";
const envConfig = env(node, {});
nodeless
Suitable to transform libraries made for Node.js to run in other JavaScript runtimes.
import { env, nodeless } from "unenv";
const envConfig = env(nodeless, {});
deno
This preset can be used to extend nodeless
to use Deno's Node.js API Compatibility (docs, docs).
[!WARNING] This preset is experimental and behavior might change!
import { env, nodeless, deno } from "unenv";
const envConfig = env(nodeless, deno, {});
cloudflare
This preset can be used to extend nodeless
to use Cloudflare Worker Node.js API Compatibility (docs).
[!WARNING] This preset is experimental and behavior might change!
[!NOTE] Make sure to enable
nodejs_compat
compatibility flag.
import { env, nodeless, cloudflare } from "unenv";
const envConfig = env(nodeless, cloudflare, {});
vercel
This preset can be used to extend nodeless
to use Vercel Edge Node.js API Compatibility (docs).
[!WARNING] This preset is experimental and behavior might change!
import { env, nodeless, vercel } from "unenv";
const envConfig = env(nodeless, vercel, {});
unenv
provides a replacement for all Node.js built-ins for cross-platform compatibility.
unenv
provides a replacement for common npm packages for cross platform compatibility.
Package | Status | Source |
---|---|---|
npm/consola | Use native console | unenv/runtime/npm/consola |
npm/cross-fetch | Use native fetch | unenv/runtime/npm/cross-fetch |
npm/debug | Mocked with console.debug | unenv/runtime/npm/debug |
npm/fsevents | Mocked | unenv/runtime/npm/fsevents |
npm/inherits | Inlined | unenv/runtime/npm/inherits |
npm/mime-db | Minimized | unenv/runtime/npm/mime-db |
npm/mime | Minimized | unenv/runtime/npm/mime |
npm/node-fetch | Use native fetch | unenv/runtime/npm/node-fetch |
npm/whatwg-url | Use native URL | unenv/runtime/npm/whatwg-url |
import MockProxy from "unenv/runtime/mock/proxy";
console.log(MockProxy().foo.bar()[0]);
The above package doesn't work outside of Node.js and neither we need any platform-specific logic! When aliasing os
to mock/proxy-cjs
, it will be auto-mocked using a Proxy Object which can be recursively traversed like an Object
, called like a Function
, Iterated like an Array
, or instantiated like a Class
.
We use this proxy for auto-mocking unimplemented internals. Imagine a package does this:
const os = require("node:os");
if (os.platform() === "windows") {
/* do some fix */
}
module.exports = function main() {
return "Hello world"
}
By aliasing os
to unenv/runtime/mock/proxy-cjs
, the code will be compatible with other platforms.
To discover other polyfills, please check ./src/runtime.
MIT
FAQs
`unenv` is a framework-agnostic system that allows converting JavaScript code to be platform agnostic and work in any environment including Browsers, Workers, Node.js, or JavaScript runtime.
The npm package unenv receives a total of 473,292 weekly downloads. As such, unenv popularity was classified as popular.
We found that unenv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.